package com.xxh.dmserver.server;

import com.alibaba.fastjson.JSON;
import com.mongodb.client.MongoCollection;
import com.xxh.common.utils.RedisUtil;
import com.xxh.common.utils.Result;
import com.xxh.dmserver.dto.DanmuDto;
import com.xxh.dmserver.entity.danmu;
import org.bson.Document;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/danmu")
public class dmServerController {

    @Autowired
    RedisUtil redisUtil;

    @Autowired
    com.xxh.dmserver.dao.danmuRepo danmuRepo;

    @Autowired
    MongoTemplate mongoTemplate;


    @GetMapping("/test")
    public String test() {
        MongoCollection<Document> movie = mongoTemplate.getCollection("movie");
        long l = movie.countDocuments();
        return "Mongdb ok " + l;
    }

    //根据视频ID获取视频弹幕
    @GetMapping("/v3")
    public Result getDanmu(@RequestParam(name = "id") String player,
                           @RequestParam(name = "max", required = false) Integer maxdanmu) {

        System.out.println("获取弹幕");
        System.out.println("id:" + player);
        //首先从缓存里取
        String data = redisUtil.get("danmku" + player);

        //data为空会返回[],算两个长度
        if ((null == data) || (data.length() == 2)) {
            //缓存没，就从DB里面取
            List<danmu> danmus = danmuRepo.findByPlayer(player);
            System.out.println(danmus);
            //马上放到缓存
            redisUtil.set("danmku" + player, JSON.toJSONString(danmus));
            //和前端规约
            List<Object> list = formatDanmu(danmus);
            Result res = Result.success().put("data", list).put("code", 0);
            return res;
        } else {
            //缓存里有
            List<danmu> danmus = JSON.parseArray(data, danmu.class);
            //和前端规约
            List<Object> list = formatDanmu(danmus);
            Result res = Result.success().put("data", list).put("code", 0);
            return res;
        }
    }


    //获取视频弹幕以列表显示
    @GetMapping("/getDamku")
    public Result getForTableList(@RequestParam(name = "bv") String player) {
        //首先从缓存里取
        String data = redisUtil.get("danmku" + player);

        //data为空会返回[],算两个长度
        if ((null == data) || (data.length() == 2)) {
            //缓存没，就从DB里面取
            List<danmu> danmus = danmuRepo.findByPlayer(player);
            //马上放到缓存
            redisUtil.set("danmku" + player, JSON.toJSONString(danmus));
            Result res = Result.success().put("data", danmus);
            return res;
        } else {
            //缓存里有
            List<danmu> danmus = JSON.parseArray(data, danmu.class);
            Result res = Result.success().put("data", danmus);
            return res;
        }
    }


    //发弹幕
    @PostMapping("/v3")
    public Result postDanmu(@RequestBody DanmuDto dmto) {
        System.out.println("发送弹幕");
        danmu danmu = new danmu();
        dmto.setPlayer(dmto.getId());
        BeanUtils.copyProperties(dmto, danmu);
        danmuRepo.save(danmu);
        //删除缓存的值，保证值是最新的
        redisUtil.delete("danmku" + danmu.getPlayer());
        return Result.success();
    }

    //弹幕对象格式化为指定的格式
    public static List<Object> formatDanmu(List<danmu> danmus) {
        List list = new ArrayList<Object>();
        //与前端数据格式规约
        for (danmu d : danmus) {
            Object[] objs = new Object[5];
            objs[0] = d.getTime();
            //弹幕浮动位置
            objs[1] = d.getType();
            objs[2] = d.getColor();
            objs[3] = d.getAuthor();
            objs[4] = d.getText();
            list.add(objs);
        }
        return list;
    }

}
